/*
 * Edit.cpp
 *
 *  Created on: 2012-10-21
 *      Author: yangentao@gmail.com
 */
#include "Edit.h"

Edit::Edit() {
}

Edit::~Edit() {
}
DWORD Edit::getDefaultExStyle() {
	return WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_CLIENTEDGE;
}
DWORD Edit::getDefaultStyle() {
	return WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL;
}
DWORD Edit::getDefaultMultiLineStyle() {
	return WS_TABSTOP | ES_LEFT | ES_AUTOHSCROLL | ES_AUTOVSCROLL | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE | ES_WANTRETURN;
}
LPCTSTR Edit::getClassName() {
	return WC_EDIT;
}
bool Edit::create(HWND parent, Rect rect, UINT id) {
	return Super::create(parent, "", rect, id);
}

void Edit::setAutoHScroll(bool autoHScroll) {
	autoHScroll ? addStyle(ES_AUTOHSCROLL) : removeStyle(ES_AUTOHSCROLL);
}
void Edit::setAutoVScroll(bool autoVScroll) {
	autoVScroll ? addStyle(ES_AUTOVSCROLL) : removeStyle(ES_AUTOVSCROLL);

}
void Edit::setTextAlign(TextAlign align) {
	if (align == textAlignLeft) {
		addStyle(ES_LEFT);
	} else if (align == textAlignRight) {
		addStyle(ES_RIGHT);
	} else if (align == textAlignCenter) {
		addStyle(ES_CENTER);
	}
}
void Edit::setLowercase(bool lowerCase) {
	if (lowerCase) {
		addStyle(ES_LOWERCASE);
	} else {
		removeStyle(ES_LOWERCASE);
	}
}
void Edit::setUppercase(bool upperCase) {
	if (upperCase) {
		addStyle(ES_UPPERCASE);
	} else {
		removeStyle(ES_UPPERCASE);
	}
}
void Edit::setMultiLine(bool multiLine) {
	if (multiLine) {
		addStyle(getDefaultMultiLineStyle());
	} else {
		removeStyle(ES_MULTILINE | ES_WANTRETURN);
	}

}
void Edit::setNoHideSelect(bool noHideSel) {
	noHideSel ? addStyle(ES_NOHIDESEL) : removeStyle(ES_NOHIDESEL);
}
void Edit::setNumber(bool num) {
	num ? addStyle(ES_NUMBER) : removeStyle(ES_NUMBER);
}
void Edit::setPassword(bool password) {
	password ? addStyle(ES_PASSWORD) : removeStyle(ES_PASSWORD);
}
void Edit::setReadOnly(bool readOnly) {
	readOnly ? addStyle(ES_READONLY) : removeStyle(ES_READONLY);
}
void Edit::setWantReturn(bool wantReturn) {
	wantReturn ? addStyle(ES_WANTRETURN) : removeStyle(ES_WANTRETURN);
}
#if (_WIN32_WINNT >= 0x0600)
void Edit::setHint(LPCWSTR hint) {
	Edit_SetCueBannerTextFocused (getWnd(), hint, TRUE);
}
WString Edit::getHint() {
	WString s(256);
	Edit_GetCueBannerText(getWnd(), s.bufferW(), 256);
	return s;
}
#endif

void Edit::showBalloonTip(LPCWSTR title, LPCWSTR text, BalloonTipIcon icon) {
	int n = TTI_INFO;
	switch (icon) {
	case editTipIconError:
		n = TTI_ERROR;
		break;
	case editTipIconInfo:
		n = TTI_INFO;
		break;
	case editTipIconWarning:
		n = TTI_WARNING;
		break;
#if (_WIN32_WINNT >= 0x0600)
		case editTipIconErrorLarge:
		n = TTI_ERROR_LARGE;
		break;
		case editTipIconInfoLarge:
		n = TTI_INFO_LARGE;
		break;
		case editTipIconWarningLarge:
		n = TTI_WARNING_LARGE;
		break;
#endif
	}
	EDITBALLOONTIP tip;
	tip.cbStruct = sizeof(tip);
	tip.pszText = text;
	tip.pszTitle = title;
	tip.ttiIcon = n;
	Edit_ShowBalloonTip(getWnd(), &tip);
}
void Edit::hideBalloonTip() {
	Edit_HideBalloonTip(getWnd());
}

bool Edit::canUndo() {
	return SendMessage(getWnd(), EM_CANUNDO, 0, 0) != 0;
}
int Edit::charFromPos(int x, int y) {
	int line = 0;
	return charFromPos(x, y, line);
}
int Edit::charFromPos(int x, int y, int& lineNO) {
	HRESULT hr = sendMessage(EM_CHARFROMPOS, 0, MAKELPARAM(x, y));
	lineNO = HIWORD(hr);
	return LOWORD(hr);
}

void Edit::clearUndoBuffer(){
	sendMessage(EM_EMPTYUNDOBUFFER);
}
void Edit::setAutoWrapWhenGetText(bool autoWrap){
	sendMessage(EM_FMTLINES, autoWrap ? TRUE: FALSE, 0);
}
